package com.yuntai.pms.model.param;

import java.io.Serializable;
import java.util.List;

/**
 * @ClassName: Pager
 * @Description: TODO
 * @author wangze@hsyuntai.com
 * @date 2017年8月1日 下午9:45:51
 */
public class Pager<T> implements Serializable {
    private static final long serialVersionUID = -5177391824609508512L;

    private boolean           result           = true;
    private String            message          = "成功";

    /**
     * 设定缺省每页的记录数
     */
    private int               pageSize         = 15;

    /**
     * 每页中存放的记录列表
     */
    private List<T>           list             = null;

    /*
     * 后台转dhtmlxGrid适配的json数据
     */
    private GridRows          gridr            = null;

    /**
     * 总记录数
     */
    private long               rowCount         = 0;

    /**
     * 总页数... 计算公式：rowCount/pageSize 或 (rowCount/pageSize) +1
     */
    private int               pageCount;

    private String            requestURI       = "";

    /**
     * 跳转的页码数,缺省值为第1页
     */
    private int               pageNo           = 1;

    public Pager() {
    }

    public Pager(boolean result, String message) {
        this.result = result;
        this.message = message;
    }

    public Pager(int rowCount) {
        this.rowCount = rowCount;
        this.pageCount = countTotalPage();
    }

    /**
     * 构造方法，
     */
    public Pager(int pageNo, int pageSize, int rowCount) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.rowCount = rowCount;
        this.pageCount = countTotalPage();
    }

    /**
     * 构造方法，构造所有
     */
    public Pager(int pageNo, int pageSize, int rowCount, List<T> list) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.rowCount = rowCount;
        this.pageCount = countTotalPage();
        this.list = list;
    }

    /**
     * 计算总页数.
     */
    private final int countTotalPage() {
        if (rowCount % pageSize == 0) {
            return (int)rowCount / pageSize;
        } else {
            return (int)rowCount / pageSize + 1;
        }
    }

    /**
     * 取每页数据容量.
     */
    public int getPageSize() {
        return pageSize;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public GridRows getGridr() {
        return gridr;
    }

    public void setGridr(GridRows gridr) {
        this.gridr = gridr;
    }

    public long getRowCount() {
        return rowCount;
    }

    public void setRowCount(long rowCount) {
        this.rowCount = rowCount;
        this.pageCount = countTotalPage();
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获取跳转页第一条数据所在数据集的位置.
     */
    public int getRowStartIndex() {
        if (pageNo > pageCount) {
            pageNo = pageCount;
        }
        if (pageNo <= 1) {
            return 0;
        } else {
            return (pageNo - 1) * pageSize;
        }
    }

    public String getRequestURI() {
        return requestURI;
    }

    public void setRequestURI(String requestURI) {
        this.requestURI = requestURI;
    }

    public boolean isResult() {
        return result;
    }

    public void setResult(boolean result) {
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Pager<T> change(boolean result, String message) {
        this.result = result;
        this.message = message;
        return this;
    }
}
